package com.itheima.stock.service.impl;

import com.itheima.stock.domain.StockWeekKDomain;
import com.itheima.stock.mapper.StockRtInfoMapper;
import com.itheima.stock.pojo.dto.StockScreenSecond;
import com.itheima.stock.service.StockRtInfoService;
import com.itheima.stock.utils.DateTimeUtil;
import com.itheima.stock.vo.resp.R;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName StockRtInfoServiceImpl
 * @Description TODO
 * @date 2023/3/4 20:01
 * @Version 1.0
 */
@Service
public class StockRtInfoServiceImpl implements StockRtInfoService {
    @Autowired
    private StockRtInfoMapper stockRtInfoMapper;


    /**
     * 查询周k
     *
     * @param code
     * @return
     */
    @Override
    public R<List<StockWeekKDomain>> getWeekK(String code) throws ParseException {
        //1.参数校验
//        if (Objects.isNull(code)) {
//            return R.error("查询失败");
//        }
        Date time = new Date();
        Integer weekCount = 20;
        Date date = new SimpleDateFormat("yyyyMMddHHmmss").parse("20220114150000");
        String dataWeekList = getDataWeek(date);
        System.out.println("20211230093500");
        ArrayList<String> weeks = new ArrayList<>();
        weeks.add(dataWeekList);
        int day = 7;
        for (int i = 0; i < weekCount; i++) {
            String timeBeforeDay = getTimeBeforeDay(dataWeekList, day);
            Date da = new SimpleDateFormat("yyyyMMddHHmmss").parse(timeBeforeDay);
            DateTime dateTime = new DateTime(da);
            DateTime lastDate4Stock = DateTimeUtil.getLastDate4Stock(dateTime);
            weeks.add(lastDate4Stock.toString());
            day += 7;
        }
        Collections.sort(weeks);
        System.out.println("打印20周的周一时间" + weeks);


        List<StockWeekKDomain> list = stockRtInfoMapper.getWeekK(code, weeks);


        return R.ok(list);
    }

    @Override
    public StockScreenSecond selectSecond(String code) {
        //上线换成当前时间
        StockScreenSecond stockScreenSecond = stockRtInfoMapper.selectOneByCodeAndDate(code);
        System.out.println(stockScreenSecond);
        return stockScreenSecond;
    }

    private String getDataWeek(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        System.out.println("所在周星期一的日期：" + sdf.format(cal.getTime()));
        return sdf.format(cal.getTime());
    }

    /**
     * 指定时间往前或往后几天
     *
     * @param time 指定时间
     * @param day  天数
     * @return
     */
    public static String getTimeBeforeDay(String time, int day) {

        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMddHHmmss").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int minute = c.get(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, minute - day);    //往前推几天
        //c.set(Calendar.DAY_OF_MONTH, minute + day);  往后推几天

        String dayafter = new SimpleDateFormat("yyyyMMddHHmmss").format(c.getTime());

        return dayafter;

    }
}